home *** CD-ROM | disk | FTP | other *** search
/ Packard Bell - Internet on a CD / internet on a cd.cdr / Internet / sites / HTML_1 / hangman.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-16  |  3.0 KB  |  99 lines

  1. #include <iostream.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "./cgi.h"
  6.  
  7. int print_form(char *, char *);
  8.  
  9. cgi_main(cgi_info *ci)
  10.    { form_entry *params = NULL;
  11.      char guess[10];
  12.      char *nnew, *reall;
  13.      int gue,i,g,num,n;
  14.  
  15.      params = get_form_entries(ci);
  16.      reall = parmval(params,"reall");
  17.      print_mimeheader("text/html");
  18.  
  19.    if(params != NULL) {    
  20.      cout << "<title> Hangman </title>"
  21.           << "<h1>The Hangman Game ...</h1>";
  22.      if (!(strcmp(parmval(params,"guess"),"done")))       /* they guessed */
  23.     { if (!(strcmp(reall,parmval(params,"nnew"))))     /* correct? */
  24.             { cout << "You were right.";}
  25.           else { cout << "You were WRONG!<p>"
  26.                       << "It was reallly %s.",reall;}
  27.           cout << "<p><p>Click <a href=""/cgi-bin/nik/hangman"">"
  28.                << "here</a> to play again.";
  29.           return 1;
  30.        }
  31.      nnew = parmval(params,"nnew");
  32.      sprintf(guess,"%s%c",parmval(params,"guess"),nnew[0]);
  33.      gue = strlen(guess); 
  34.      num = strlen(reall);
  35.      cout << "<p>So far you have guessed:<p>";
  36.      n=0;
  37.      for (i=0;i<num;i++)  
  38.        { for (g=0;g<gue;g++) 
  39.         { if (guess[g] == reall[i]) 
  40.              { cout << reall[i];
  41.                        g=-1;
  42.                n++;
  43.                break;
  44.                }
  45.         }
  46.              if (g==-1) continue;
  47.              cout << "_ ";
  48.          }
  49.      cout << "<p>";
  50.      if ((gue-n) < 6) {
  51.         cout << "Please type your next guess<p>"
  52.          << "You have " << (6-(gue-n)) << " guesse(s) left!<p>";
  53.         print_form(reall,guess);
  54.         }
  55.      else{
  56.     cout << "Sorry the game is over.<p>"
  57.          << "The correct answer is " << reall << "<p>"
  58.          << "click <a href=""/cgi-bin/nik/hangman"">"
  59.          << "here</a> to play again.";
  60.         }
  61.      }
  62. else {
  63.     cout << "<title> Hangman </title>"
  64.          << "<h1> Hangman Game.</h1><br>";
  65.     reall = "first";
  66.     num = strlen(reall);
  67.     for (i=1;i<=num;i++)
  68.      { cout << "_ ";
  69.       }
  70.     cout << "<p>"
  71.          << "Type in your first guess at the above unknown word.<p>";
  72.     print_form(reall,"");
  73.     }
  74.  }
  75.  
  76. print_form(char *reall,char *guess)
  77. { cout << "<form method=""post"" action=""/cgi-bin/nik/hangman"">"
  78.        << "<input name = ""nnew""><p>"
  79.        << "<input type=""hidden"" name=""guess"" value="""
  80.        << guess << """>"
  81.        << "<input type=""hidden"" name=""reall"" value="""
  82.        << reall << """>"
  83.        << "<INPUT TYPE=""reset"" VALUE=""Clear"">"
  84.        << "<input type=""submit"" value=""Continue"">"
  85.        << "</form>"
  86.        << "Or if you are daring type in what"
  87.        << " you think that the word is.<p>"
  88.        << "<form method=""post"" action=""/cgi-bin/nik/hangman"">"
  89.        << "<input name=""nnew""><p>"
  90.        << "<input type=""hidden"" name=""guess"" value=""done"">"
  91.        << "<input type=""hidden"" name=""reall"" value="""
  92.        << reall << """>"
  93.        << "<INPUT TYPE=""reset"" VALUE=""Clear"">"
  94.        << "<input type=""submit"" value=""Guess It"">"
  95.        << "</form> </html>";
  96.        return 1;
  97.        }
  98.  
  99.